package com.skyline.common.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.skyline.base.type.Authority;
import com.skyline.user.model.Fan;
import com.skyline.user.model.User;
import com.skyline.user.service.AttentionService;

/**
 * 权限的Util
 * 
 * @author Jairus Chan
 * @version 0.1
 * */
public class AuthorityUtil {

	private static AttentionService attentionService;

	/**
	 * 初始化权限矩阵
	 * 
	 * @param request
	 *            HttpServletRequest 如果为null则取ThreadLocal中的request
	 * @param userid
	 *            用户本人，对于本人而言，访问权限为Authority.PRIVATE
	 * @param idols
	 *            用户的偶像，访问他的偶像时，访问权限为Authority.ONLY_TO FANS
	 */
	public static void initalAuthorityMatrix(HttpServletRequest request, Long userid, List<Fan> idols) {
		Map<Long, Integer> authorityMatrix = new HashMap<Long, Integer>();
		for (Fan idol : idols) {
			// addAuthority(userid, Authority.PRIVATE, authorityMatrix,
			// session);
			authorityMatrix.put(idol.getIdolId(), Authority.ONLY_TO_FANS);
		}
		addAuthority(userid, Authority.PRIVATE, authorityMatrix, request);
	}

	/**
	 * 获取访问用户的权限
	 * 
	 * @param request
	 *            HttpServletRequest 如果为null则取ThreadLocal中的request
	 * @param userid
	 *            需要访问的用户
	 * @return
	 */
	public static Integer getAuthority(HttpServletRequest request, Long userId) {
		Map<Long, Integer> authorityMatrix = getAuthorityMatrix(request);
		User curUser = (User) WebHelper.getSessionAttribute(request, Constant.SESSION_USER);
		if (authorityMatrix == null || curUser == null) {
			return Authority.PUBLIC;
		}
		Integer authority = authorityMatrix.get(userId);
		// 如果authority == null，则表示被请求的用户不在请求用户的权限矩阵中
		if (authority != null) {
			return authority;
		}
		// 同城暂时不用
		// if (areTheyInSameCity()) {
		// return Authority.
		// } else
		if (isFan(curUser.getId(), userId)) {
			return Authority.ONLY_TO_FANS;
		} else if (isManager()) {
			return Authority.ADMIN;
		} else {
			return Authority.PUBLIC;
		}
	}

	/**
	 * 获取整个权限矩阵
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @return
	 */
	public static Map<Long, Integer> getAuthorityMatrix(HttpServletRequest request) {
		@SuppressWarnings("unchecked")
		Map<Long, Integer> authorityMatrix = (Map<Long, Integer>) WebHelper.getSessionAttribute(request, Constant.SESSION_AUTHORITY_MATRIX);
		return authorityMatrix;
	}

	private static void addAuthority(Long userid, Integer authority, Map<Long, Integer> authorityMatrix, HttpServletRequest request) {
		authorityMatrix.put(userid, authority);
		WebHelper.setSessionAttribute(request, Constant.SESSION_AUTHORITY_MATRIX, authorityMatrix);
	}

	// @Deprecated
	// private static boolean areTheyInSameCity() {
	// return false;
	// }

	private static boolean isFan(Long curUserId, Long userId) {
		if (attentionService == null) {
			attentionService = SpringUtils.getBean(AttentionService.class);// Spring中AttentionService只有一个
		}
		return attentionService.isFan(curUserId, userId);
	}

	private static boolean isManager() {
		return false;
	}
}
